<template>
  <div class="manager-center-page">
    <div>
      <span class="title">自动分发</span>
      <el-switch
        v-model="type"
        active-color="rgba(79, 138, 255, 1)"
        >
      </el-switch>
    </div>
    <div>
      <span class="descript">开启自动分发：审核通过时，立即把内容自动分发给所有在线终端</span>
      <span class="descript">关闭自动分发：审核通过后，在设置时间段内把内容自动分发给所有在线终端</span>
    </div>
    <div class="timer" v-if="!type">
      <span class="title">自定义时间段</span>
      <el-time-picker
          is-range
          v-model="time"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          placeholder="选择时间范围">
        </el-time-picker>
    </div>
    <div class="container">
      <el-button type="primary" @click="save">保存</el-button>
    </div>
  </div>
</template>

<script>
import * as service from './service'
export default {
  data() {
    return {
      id: null,
      type: true,
      time: [],
    }
  },
  mounted() {
    this.getConfig();
  },
  methods: {
    save(){
      if(!this.type && this.time.length < 2) {
        this.$message.success('时间不能为空')
      }
      let beginTime = "";
      if(!this.type && this.time[0] != null) {
        let hour = String(this.time[0].getHours()).padStart(2, '0');
        let min = String(this.time[0].getMinutes()).padStart(2, '0');
        let second = String(this.time[0].getSeconds()).padStart(2, '0');
        beginTime = hour+":"+min+":"+second;
      }
      let endTime = "";
      if(!this.type && this.time[1] != null) {
        let hour = String(this.time[1].getHours()).padStart(2, '0');
        let min = String(this.time[1].getMinutes()).padStart(2, '0');
        let second = String(this.time[1].getSeconds()).padStart(2, '0');
        endTime = hour+":"+min+":"+second;
      }
      let params = {
        id: this.id,
        type: this.type ? 1 : 0,
        begin_time: beginTime != "" ? beginTime : null,
        end_time: endTime != "" ? endTime : null,
      }
      service.saveSendFileConfig(params).then(() => {
        this.$message.success('保存成功')
      })
    },
    getConfig() {
      service.getSendFileConfig().then((res) => {
        let data = res.data;
        if(data) {
          this.type = data.type == 1 ? true : false;
          this.id = data.id;
          let date = new Date();
          let year = date.getFullYear();
          let month = date.getMonth() + 1; // JavaScript中的月份是从0开始的
          let day = date.getDate();
          let begin = data.begin_time;
          let beginDate = new Date(year, month, day, begin.split(":")[0], begin.split(":")[1], begin.split(":")[2]);
          let end = data.end_time;
          let endDate = new Date(year, month, day, end.split(":")[0], end.split(":")[1], end.split(":")[2]);
          this.time.push(beginDate);
          this.time.push(endDate);
        }
      })
    },
  }
}
</script>

<style scoped lang="scss">
  .manager-center-page{
    width: 100%;
    height: 92vh;
    background: white;
    padding: 15px;
    position: relative;

    .title{
      display: inline-block;
      margin-right: 50px;
      font-size: .28rem;
      vertical-align: middle;
      color: #444;
    }

    .timer{
      margin-top: 30px;
    }

    .container{
      margin-top: 60px;
      width: 100%;
      text-align: center;

      button{
        width: 120px;
      }
    }


    .descript{
      display: inline-block;
      margin-top: 10px;
      font-size: .25rem;
      color: #aaa;
      font-weight: 350;
    }

    ::v-deep .el-switch.is-checked .el-switch__core {
      background-color: rgba(79, 138, 255, 1)!important;
    }
  }

</style>
